if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 4.7.0 AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.7.9)
  return()
endif()

if(CMAKE_VERSION GREATER_EQUAL 3.11)
  include(FetchContent)
  FetchContent_Declare(doctest GIT_REPOSITORY https://github.com/doctest/doctest.git GIT_TAG v2.4.9)
  FetchContent_GetProperties(doctest)
  if(NOT doctest_POPULATED)
    set(DOCTEST_NO_INSTALL  TRUE)
    FetchContent_Populate(doctest)
    add_subdirectory("${doctest_SOURCE_DIR}" "${doctest_BINARY_DIR}")
    include("${doctest_SOURCE_DIR}/scripts/cmake/doctest.cmake")
    unset(DOCTEST_NO_INSTALL)
  endif()
else()
  find_package(doctest QUIET)
  if(NOT doctest_FOUND)
    message(WARNING "CMake is too old to use FetchContent and doctest is not found !\nSkipping tests !")
    return()
  endif()
  include(doctest)
endif()

add_executable(test_terminal test_terminal.cpp)
target_link_libraries(test_terminal PRIVATE doctest::doctest cpp-terminal::cpp-terminal)
doctest_discover_tests(test_terminal)

add_executable(AttachConsole WIN32 attach_console.test.cpp)
target_link_libraries(AttachConsole PRIVATE doctest::doctest cpp-terminal::cpp-terminal)

add_executable(AttachConsoleMinimal WIN32 attach_console_minimal.test.cpp)
target_link_libraries(AttachConsoleMinimal PRIVATE doctest::doctest cpp-terminal::cpp-terminal)

add_executable(file.test file.test.cpp)
target_link_libraries(file.test PRIVATE doctest::doctest cpp-terminal::cpp-terminal)
doctest_discover_tests(file.test)

add_executable(Key key.test.cpp)
target_link_libraries(Key PRIVATE doctest::doctest cpp-terminal::cpp-terminal)
doctest_discover_tests(Key)

add_executable(IOStream iostream.test.cpp)
target_link_libraries(IOStream PRIVATE cpp-terminal::cpp-terminal)

if(MINGW OR MSYS)
else()
add_executable(Args args.test.cpp)
target_link_libraries(Args PRIVATE doctest::doctest cpp-terminal::cpp-terminal)
doctest_discover_tests(Args)
## Needed to test args
add_test(NAME Args COMMAND Args Bonjour Hello 你好)
endif()

if (WIN32)
  find_program(POWERSHELL_EXECUTABLE NAMES powershell DOC "PowerShell command")
  if (NOT ${POWERSHELL_EXECUTABLE} STREQUAL "POWERSHELL_EXECUTABLE-NOTFOUND")
    add_test(NAME tests COMMAND ${POWERSHELL_EXECUTABLE} -NoProfile -NonInteractive ${CMAKE_SOURCE_DIR}/ci/examples.ps1 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
  endif ()
endif ()
